﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Practices.EnterpriseLibrary.Validation;
using Microsoft.Practices.EnterpriseLibrary.Validation.Validators;


namespace Domain
{
    //based on David Hayden
    // http://davidhayden.com/blog/dave/archive/2006/12/28/ValidationApplicationBlockBusinessLayerDataAccessLayerIntegration.aspx
    //
    public abstract class DomainObject<T> where T : class
    {
        protected Validator<T> _validationService;

        private bool _isDirty = true;
        public virtual bool IsDirty
        {
            get { return _isDirty; }
        }
        #region Constructors
        public DomainObject()
        {
            _validationService = ValidationFactory.CreateValidator<T>();
        }
        public DomainObject(Validator<T> validationService)
        {
            if (validationService == null)
                throw new ArgumentNullException("validationService", "Validation Service cannot be null");
            _validationService = validationService;
        }
        #endregion
        public virtual ValidationResults Validate()
        {
            return _validationService.Validate(this);
        }
        public bool IsValid()
        {
            return Validate().IsValid;
        }
        public bool IsSavable()
        {
            return IsDirty && IsValid();
        }
    }

}
